package com.whu.edu.zqm.assignments4.controller;

import com.whu.edu.zqm.assignments4.entity.GoodsItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jiaxy
 * 待办事项控制器类。 负责提供API。可以在这个类中做请求响应数据的转换、验证，但不要写具体业务逻辑。
 */
@Api(description = "待办事项管理")
@RestController
@RequestMapping("todos")
public class GoodsController {
    @Autowired
    GoodService goodsService;


    @ApiOperation("根据Id查询商品信息")
    @GetMapping("/{id}")
    public ResponseEntity<GoodsItem> getGood(@ApiParam("商品Id")@PathVariable long id){
        GoodsItem result = goodsService.getGoods(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }

    @ApiOperation("根据条件查询待办事项")
    @GetMapping("")
    public ResponseEntity<List<GoodsItem>> findGoods(@ApiParam("待办事项名称")String name,@ApiParam("剩余数量")int num){
        List<GoodsItem> result = goodsService.findGoods(name,num);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("添加待办事项")
    @PostMapping("")
    public ResponseEntity<GoodsItem> addGoods(@RequestBody GoodsItem good){
        GoodsItem result = goodsService.addGoods(good);
        return ResponseEntity.ok(result);
    }


    @ApiOperation("修改待办事项")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateGoods(@PathVariable long id,@RequestBody GoodsItem good){
        goodsService.updateGoods(id,good);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("删除待办事项")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable long id){
        goodsService.deleteGoods(id);
        return ResponseEntity.ok().build();
    }

}
